<template>
	<main-container>
		<!-- 搜索 -->
		<el-form :model="state.queryParams" ref="queryRef" :inline="true" v-show="showSearch">
			<el-form-item label="店铺名称">
				<el-input v-model="state.queryParams.name" clearable></el-input>
			</el-form-item>
			<el-form-item label="审核状态">
				<el-select v-model="state.queryParams.verifyStatus" clearable>
					<el-option v-for="item in shop_verify_status" :key="item.value" :label="item.label" :value="item.value"></el-option>
				</el-select>
			</el-form-item>
			<el-form-item>
				<el-button type="primary" @click="initPage"
					><el-icon> <ele-Search /> </el-icon>搜索</el-button
				>
				<el-button @click="resetQuery"
					><el-icon> <ele-Refresh /> </el-icon>重置</el-button
				>
			</el-form-item>
		</el-form>
		<Form ref="formRef" @initPage="initPage"></Form>
		<!-- 工具栏 -->
		<div class="hx-table-toolbar">
			<div></div>
			<right-toolbar :searchBtn="true" :refreshBtn="true" @search="showSearch = !showSearch" @refresh="initPage"></right-toolbar>
		</div>
		<!-- 列表 -->
		<el-table v-loading="loading" :data="state.tableData" border>
			<el-table-column prop="name" label="名称" align="center" show-overflow-tooltip></el-table-column>
			<el-table-column prop="photoUrl" label="logo" align="center" show-overflow-tooltip>
				<template #default="scope"
					><el-image
						v-if="scope.row.logoUrl"
						style="width: 60px; height: 60px"
						:src="scope.row.logoUrl"
						:preview-src-list="[scope.row.logoUrl]"
						fit="cover"
						:preview-teleported="true"
					></el-image
				></template>
			</el-table-column>
			<el-table-column prop="telephone" label="联系电话" align="center"></el-table-column>
			<el-table-column prop="detailAddress" label="地址" align="center"></el-table-column>
			<el-table-column prop="verifyStatus" label="审核状态" align="center">
				<template #default="scope">
					<dict-tag :options="shop_verify_status" :value="scope.row.verifyStatus"></dict-tag>
				</template>
			</el-table-column>
			<el-table-column prop="createTime" label="创建时间" width="180" />
			<el-table-column label="操作" width="200" align="center" fixed="right">
				<template #default="scope">
					<el-button link type="primary" @click="verify(scope.row)" v-auth="'shop:shopapply:verify'">{{
						scope.row.verifyStatus == '0' ? '审核' : '详情'
					}}</el-button>
					<el-button link type="danger" @click="del(scope.row.shopId)" v-auth="'shop:shopapply:del'"
						><el-icon> <ele-Delete /> </el-icon>删除</el-button
					>
				</template>
			</el-table-column>
		</el-table>
		<!-- 分页 -->
		<pagination
			v-model:total="state.page.total"
			v-model:currentPage="state.page.currentPage"
			v-model:pageSize="state.page.pageSize"
			@changePage="initPage"
		></pagination>
	</main-container>
</template>
<script setup lang="ts" name="apply">
import { ref, reactive, defineAsyncComponent } from 'vue';
import { getPage, delObj } from '/@/api/shop/shopapply';
import { ElMessageBox, ElMessage } from 'element-plus';
import { useDict } from '/@/utils/dict';
const Form = defineAsyncComponent(() => import('./form.vue'));

const { shop_verify_status } = useDict('shop_verify_status');
const state = reactive({
	queryParams: {
		name: '',
		verifyStatus: '',
	},
	page: {
		total: 0,
		currentPage: 1,
		pageSize: 10,
		descs: 'create_time',
	},
	tableData: [],
});
const showSearch = ref(true);
const loading = ref(false);
const formRef = ref();
const initPage = async () => {
	loading.value = true;
	let params = {
		current: state.page.currentPage,
		size: state.page.pageSize,
		descs: state.page.descs,
	};
	await getPage(Object.assign(params, state.queryParams))
		.then((response) => {
			state.tableData = response.data.data.records;
			state.page.total = response.data.data.total;
			loading.value = false;
		})
		.catch(() => {
			loading.value = false;
		});
};
/**
 * 重置搜索表单
 */
const resetQuery = () => {
	state.queryParams = { name: '', verifyStatus: '' };
};
/**
 * 删除按钮
 */
const del = (id: string) => {
	ElMessageBox.confirm('此操作将删除该申请单，是否继续?', '提示', {
		confirmButtonText: '确认',
		cancelButtonText: '取消',
		type: 'warning',
	}).then(() => {
		delObj(id)
			.then(() => {
				ElMessage.success('删除成功');
				initPage();
			})
			.catch(() => {});
	});
};
const verify = (row: any) => {
	formRef.value.initForm(row);
};
initPage();
</script>
